package com.itheima.web.listener;

import com.itheima.pojo.User;
import com.itheima.service.LoginDataService;

import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import java.util.Date;

@WebListener
public class LogoutListener implements HttpSessionAttributeListener {
    private LoginDataService service = new LoginDataService();

    @Override
    public void attributeAdded(HttpSessionBindingEvent httpSessionBindingEvent) {

    }

    @Override
    public void attributeRemoved(HttpSessionBindingEvent httpSessionBindingEvent) {
        if (httpSessionBindingEvent.getName().equals("loginDataId")) {
            Date logoutTime = new Date();
            Integer loginDataId = (Integer) httpSessionBindingEvent.getValue();

            service.updateLogoutTime(logoutTime, loginDataId);
        }
    }

    @Override
    public void attributeReplaced(HttpSessionBindingEvent httpSessionBindingEvent) {

    }
}
